# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models


class FapiaoVatProduct(models.Model):
    _name = "fp.vat.product"
    _description = "Vat Product"
    _order = 'code'

    parent_id = fields.Many2one(
        'fp.vat.product', 'Parent Vat Product', index=True, ondelete='cascade')
    parent_path = fields.Char(index=True, unaccent=False)
    child_id = fields.One2many(
        'fp.vat.product', 'parent_id', 'Child Vat Products')

    name = fields.Char(string='Name', required=True, index=True, copy=False)
    code = fields.Char(string='Code', required=True, index=True, copy=False)
    shortname = fields.Char(
        string='Shortname', required=True, index=True, copy=False, help="票面信息")

    ratio_str = fields.Char(string='税率描述')
    vat_special = fields.Char(string='增值税特殊管理')

    grouped = fields.Boolean('Grouped', default=False, help="")
    active = fields.Boolean('Active', default=True, help="")

    note = fields.Text(string='Description', help="备注")

    @api.model_create_multi
    def create(self, vals_list):
        for vals in vals_list:
            code = vals['code']
            vals['code'] = code.ljust(19, '0')
        return super().create(vals_list)

    def write(self, vals):
        code = vals.get('code')
        if code:
            vals['code'] = code.ljust(19, '0')

        return super().write(vals)

    @api.model
    def find_or_create(self, domain, values, order=None):
        vat_product = self.search(domain, order=order, limit=1)

        if not vat_product:
            vat_product = self.create(values)

        return vat_product

    @api.model
    def lookup_for_in(self, values):
        code = values['code']
        shortname = values['shortname']

        if code:
            return self.find_or_create(
                [('code', '=', code)],
                {
                    'code': code,
                    'name': shortname,
                    'shortname': shortname
                }
            )
        elif shortname:
            return self.find_or_create(
                [('shortname', '=like', shortname)],
                {
                    'code': code,
                    'name': shortname,
                    'shortname': shortname
                },
                order='code'

            )
        else:
            return self


class FapiaoProduct(models.Model):
    _name = "fp.product"
    _description = "Fapiao Product"
    _order = 'code'

    def _default_tax_id(self):
        return self.env.ref('fapiao_base.tax_13')

    active = fields.Boolean('Active', default=True, help="")

    fp_partner_id = fields.Many2one(
        'fp.partner', string='Partner', index=True, ondelete='restrict')

    in_out = fields.Selection(
        selection=[
            ('out', '销项'),
            ('in', '进项'),
        ], string='In or Out',
        required=True, index=True, default='out')

    vat_product_id = fields.Many2one(
        'fp.vat.product', 'Vat Product', index=True, ondelete='restrict',
        help="销项自己开的发票, 已知编码. 进项发票查验后可能为空")

    name = fields.Char(
        string='Name', required=True, index=True, copy=False, help="票面信息")

    vat_product_code = fields.Char(
        string='Var Product Code', related='vat_product_id.code', store=True, help="票面信息")
    vat_product_name = fields.Char(
        string='Var Product Name', related='vat_product_id.shortname', store=True, help="票面信息")

    code = fields.Char(string='Code', copy=False)
    spec = fields.Char(string='Spec', help="票面信息")
    uom = fields.Char(string='Uom', help="票面信息")

    price_unit = fields.Float('Unit Price', required=True, default=0.0)

    tax_ratio = fields.Float(
        string='tax_ratio', related='tax_id.tax_ratio')

    tax_id = fields.Many2one(
        'fp.tax', string='Tax Ratio Set', required=True,
        default=_default_tax_id,
        help="销项开票时, 取该字段为 默认税率")

    note = fields.Text(string='Description', help="备注")

    @api.model
    def lookup_for_in(self, values):
        vat_product_code = values['vat_product_code']
        vat_product_name = values['vat_product_name']

        vat_product_obj = self.env['fp.vat.product']

        vat_product = vat_product_obj.lookup_for_in({
            'code': vat_product_code,
            'shortname': vat_product_name
        })

        vat_product_id = vat_product.id

        product_name = values['product_name']
        product_spec = values['product_spec']
        product_uom = values['product_uom']
        price_unit = values['price_unit']
        fp_partner_id = values['fp_partner_id']

        domain = [
            ('in_out', '=', 'in'),
            ('fp_partner_id', '=', fp_partner_id),
            ('vat_product_id', '=', vat_product_id),
            ('name', '=', product_name),
            ('spec', '=', product_spec or False),
            ('uom', '=', product_uom or False),
        ]

        product = self.search(domain, limit=1)

        if not product:
            product = self.create({
                'in_out': 'in',
                'fp_partner_id': fp_partner_id,
                'vat_product_id': vat_product_id,
                'name': product_name,
                'spec': product_spec or False,
                'uom': product_uom or False,
                'price_unit': price_unit or 0.0
            })

        return product
